package com.example.mini_notepad.activity;

import java.util.Date;

import com.example.mini_notepad.R;
import com.example.mini_notepad.db.NoteDBAdapter;
import com.example.mini_notepad.util.PropertyConstants;
import com.example.mini_notepad.util.Util;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TextNoteFragment extends Fragment {
	
	static EditText edtNote;
	static NoteDBAdapter adapter;
	static Context context;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
		View rootView = inflater.inflate(R.layout.text_note_fragment, container, false);
		
		TextView tvFont = (TextView) rootView.findViewById(R.id.tvCharSize);
		tvFont.setOnClickListener(new OnFontClickListener());
		
		TextView tvUndo = (TextView) rootView.findViewById(R.id.tvUndo);
		tvUndo.setOnClickListener(new OnUndoClickListener());
		
		TextView tvSave = (TextView) rootView.findViewById(R.id.tvSave);
		tvSave.setOnClickListener(new OnSaveClickListener());
		
		TextView tvShare = (TextView) rootView.findViewById(R.id.tvShare);
		tvShare.setOnClickListener(new OnShareClickListener());
		
		edtNote = (EditText) rootView.findViewById(R.id.edtTextNote);
		edtNote.setTextSize(TypedValue.COMPLEX_UNIT_PX, PropertyConstants.FONT_SIZE_NORMAL);
		
		context = getActivity();
		
		return rootView;		
	}
	
	private static class OnFontClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub	
			//Toast.makeText(v.getContext(), edtNote.getTextSize()+"", Toast.LENGTH_LONG).show();
			if((int)(edtNote.getTextSize()) == PropertyConstants.FONT_SIZE_SMALL){
				edtNote.setTextSize(TypedValue.COMPLEX_UNIT_PX, PropertyConstants.FONT_SIZE_NORMAL);
			}else if((int)(edtNote.getTextSize()) == PropertyConstants.FONT_SIZE_NORMAL){
				edtNote.setTextSize(TypedValue.COMPLEX_UNIT_PX, PropertyConstants.FONT_SIZE_LARGE);
			}else{
				edtNote.setTextSize(TypedValue.COMPLEX_UNIT_PX, PropertyConstants.FONT_SIZE_SMALL);
			}
		}
		
	}
	
	private static class OnUndoClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			edtNote.setText("");
		}
		
	}
	
	private static class OnSaveClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//String content = edtNote.getText().toString();			
			if(context != null){
				String title = edtNote.getText().toString();
				String content = edtNote.getText().toString();
				if(content.equals("")){
					Toast.makeText(context, Util.getStringValue(R.string.emptyContentMsg),
							Toast.LENGTH_SHORT).show();
					return;
				}
				adapter = new NoteDBAdapter(context);
				
				short type = PropertyConstants.NOTE_TYPE_TEXT;
				String createTime = Util.getTimeString(new Date());
				String notifyTime = Util.getTimeString(new Date());
				adapter.open();
				adapter.insertNote(title, content, type, createTime, notifyTime);
				adapter.close();				
			}else{
				Log.w("OnSaveClickListener.onClick()", "Failed to construct DBAdapter.");
				return;
			}
			edtNote.setText("");
			Toast.makeText(context, Util.getStringValue(R.string.saveSuccMsg),
					Toast.LENGTH_SHORT).show();
		}
		
	}
	
	private static class OnShareClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			Intent intent = new Intent(Intent.ACTION_SEND);
			Bundle data = new Bundle();
		}
		
	}

}
